// 查询资源

import { createCanvas } from 'canvas'
import { segment } from 'oicq'
import { ResourceConstant, Shard } from 'screeps-simple-api'
import { drawText, splitNum } from '../../../utils/base'
import {
    barsRes,
    baseRes,
    b_blueRes,
    b_greenRes,
    b_greyRes,
    b_pinkRes,
    b_witheRes,
    b_yellowRes,
    c_blueRes,
    c_greenRes,
    c_greyRes,
    c_pinkRes,
    c_yellowRes,
    powerRes,
    RES_COLOR_MAP,
} from '../constant'
import { ICommandHandler } from '../types'

const resourceQuery: ICommandHandler = {
    commandName: '/res',
    async execute(sender, api) {
        try {
            if (!sender.command) return
            const [username] = sender.command.params
            if (!username) {
                sender.reply('请输入玩家名')
                return
            }
            // 查找该玩家id
            const userInfo = await api.getUserInfoByUserName(username)
            if (!userInfo.ok || !userInfo.user) {
                return sender.reply('未找到该玩家')
            }
            const roomsInfo = await api.getRooms(userInfo.user._id)
            if (!roomsInfo.ok) {
                return sender.reply('未找到该玩家房间信息')
            }
            sender.reply('正在查询...')
            const requests = []
            const resStats: {
                [key in ResourceConstant]?: number
            } = {}
            for (const shardName in roomsInfo.shards) {
                const shard = shardName as Shard
                for (const room of roomsInfo.shards[shard]) {
                    // 请求该房间对象
                    requests.push(api.getRoomObject(room, shard))
                }
            }
            Promise.all(requests).then((responses) => {
                let success = true
                for (const res of responses) {
                    if (!res.ok) {
                        success = false
                        return
                    }
                    for (const object of res.objects) {
                        if (object.type !== 'storage' && object.type !== 'terminal' && object.type !== 'factory')
                            continue
                        for (const resType in object.store) {
                            const type = resType as ResourceConstant
                            resStats[type] = (resStats[type] || 0) + object.store[type]
                        }
                    }
                }
                if (!success) return sender.reply('查询失败')

                // 绘图
                const gap = 100
                const width = 9 * gap
                const height = 520
                const canvas = createCanvas(width, height)
                const ctx = canvas.getContext('2d')
                ctx.fillStyle = '#2b2b2b'
                ctx.fillRect(0, 0, width, height)
                ctx.font = '14px'

                drawText(ctx, 'baseRes', 10, 15, '#fff')
                baseRes.forEach((type, index) => {
                    drawText(
                        ctx,
                        `${type}\n${splitNum(resStats[type as ResourceConstant] || 0)}`,
                        30 + index * gap,
                        30,
                        RES_COLOR_MAP[type],
                    )
                })

                drawText(ctx, 'barsRes', 10, 65, '#fff')
                barsRes.forEach((type, index) => {
                    drawText(
                        ctx,
                        `${type}\n${splitNum(resStats[type as ResourceConstant] || 0)}`,
                        30 + index * gap,
                        80,
                        RES_COLOR_MAP[type],
                    )
                })

                drawText(ctx, 'powerRes', 10, 115, '#fff')
                powerRes.forEach((type, index) => {
                    drawText(
                        ctx,
                        `${type}\n${splitNum(resStats[type as ResourceConstant] || 0)}`,
                        30 + index * gap,
                        130,
                        RES_COLOR_MAP[type],
                    )
                })

                drawText(ctx, 'goods', 10, 165, '#fff')
                for (const [y, res] of [c_greyRes, c_blueRes, c_yellowRes, c_pinkRes, c_greenRes].entries()) {
                    res.forEach((type, index) => {
                        drawText(
                            ctx,
                            `${type}\n${splitNum(resStats[type as ResourceConstant] || 0)}`,
                            30 + index * gap,
                            180 + y * 30,
                            RES_COLOR_MAP[type],
                        )
                    })
                }

                drawText(ctx, 'labRes', 10, 335, '#fff')
                for (const [y, res] of [
                    b_greyRes,
                    b_blueRes,
                    b_yellowRes,
                    b_pinkRes,
                    b_greenRes,
                    b_witheRes,
                ].entries()) {
                    res.forEach((type, index) => {
                        drawText(
                            ctx,
                            `${type}\n${splitNum(resStats[type as ResourceConstant] || 0)}`,
                            30 + index * gap,
                            350 + y * 30,
                            RES_COLOR_MAP[type],
                        )
                    })
                }

                const img = canvas.toBuffer('image/png')
                sender.reply(segment.image(img))
            })
        } catch (error) {
            sender.reply('出错啦')
        }
    },
}

export default resourceQuery
